layui.use(['element', 'layer', 'flow'], function(){
    var element = layui.element,
        layer = layui.layer,
        flow = layui.flow,
        $ = layui.$;

    //监听折叠
    element.on('collapse(test)', function(data){
        layer.msg('展开状态：'+ data.show);
    });
    //获取Top数据
    $.ajax({
        type: 'post',
        url: '/Class/notice/getTop',
        dataType: 'json',
        success: (res) =>{
            if(res.code === 0){
                let d = res.data;
                for(let i = 0; i < d.length; i++){
                    let html = `
                    <div class="layui-col-md6">
                        <div class="layui-card">
                            <div class="layui-card-header">${d[i].title}</div>
                            <div class="layui-card-body">
                                <p><span class="aaa">简介:</span><span>${d[i].info}</span></p>
                                <div>${d[i].content}</div>
                                <p><span class="aaa">时间:</span><span>${util.getTime(d[i].time)}</span></p>
                            </div>
                        </div>
                        <div class="${d[i].type ? 'top':'top2'}">
                            <span>${d[i].type ? '学院':'学校'}</span>
                        </div>
                    </div>
                    `;
                    $('#top').append(html);
                }
            }else layer.msg('系统繁忙');
        }
    })
    //流加载
    flow.load({
        elem: '#demo' //指定列表容器
        ,isAuto: false //手动加载
        ,isLazyimg: true
        ,done: function(page, next){ //到达临界点（默认滚动触发），触发下一页
            $.ajax({
                type:'post',
                url:'/Class/notice/getPage',//获取总页数
                dataType: 'json',
                success:(res) =>{
                    if (res.code === 0)
                    {
                        let pages = res.count;
                        $.ajax({
                            type:'post',
                            url:'/Class/notice/getAllNotices', //获取当前页数据
                            data: {"page": parseInt(page+1), "limit": 4},
                            dataType:'json',
                            success:(res) =>{
                                if (res.code === 0)
                                {
                                    let d = res.data;
                                    //模拟数据插入
                                    setTimeout(function(){
                                        let lis = [];
                                        let num = 4;
                                        if (d.length < 4){
                                            num = d.length;
                                        }
                                        for(let i = 0; i <num; i++){
                                            let html = `
                                                <div class="layui-colla-item">
                                                    <h2 class="layui-colla-title"  onclick="changeShow(this)">${d[i].title}
                                                        <i class="layui-icon layui-colla-icon"></i></h2>
                                                    <div class="layui-colla-content">
                                                        <div><p><span class="aaa">简介:</span>${d[i].info}</p>
                                                        <div>${d[i].content}</div>
                                                        <p><span class="aaa">时间:</span>${d[i].time}</p></div>
                                                    </div>
                                                </div>
                                              `;
                                            lis.push(html);
                                        }
                                        //执行下一页渲染，第二参数为：满足“加载更多”的条件，即后面仍有分页
                                        //pages为Ajax返回的总页数，只有当前页小于总页数的情况下，才会继续出现加载更多
                                        next(lis.join(''), page < pages); //假设总页数为 10
                                    }, 500);
                                }
                            }
                        });
                    }
                    else layer.msg("系统繁忙");
                }
            });
        }
    });
});
//切换卡片折叠状态
function changeShow(dom) {
    let $ = layui.$;
    $(dom).next().toggleClass('layui-show');
}